---
import '../styles/theme.css';
import '../styles/index.css';
import HeadCommon from '../components/HeadCommon.astro';
import HeadSEO from '../components/HeadSEO.astro';
import Header from '../components/Header/Header.astro';
import PageContent from '../components/PageContent/PageContent.astro';
import LeftSidebar from '../components/LeftSidebar/LeftSidebar.astro';
import RightSidebar from '../components/RightSidebar/RightSidebar.astro';

import * as CONFIG from '../config';
import type { MarkdownHeading } from 'astro';
import Footer from '../components/Footer/Footer.astro';

type Props = {
  frontmatter: CONFIG.Frontmatter;
  headings: MarkdownHeading[];
};

const { frontmatter = {}, headings, rightSidebar } = Astro.props as Props;
const canonicalURL = `${new URL(Astro.url.pathname, Astro.site)}`.replace(/\/$/, '');
const currentPage = Astro.url.pathname;
let currentFile = `src/pages${currentPage.replace(/\/$/, '')}`;
if (Astro.props.file) currentFile += `.${Astro.props.file.split('.').pop()}`;
const githubEditUrl = `${CONFIG.GITHUB_EDIT_URL}/${currentFile}`;
---

<html
  dir={frontmatter.dir ?? 'ltr'}
  lang={frontmatter.lang ?? 'en-us'}
  class="initial"
>
  <head>
    <HeadCommon />
    <HeadSEO frontmatter={frontmatter} canonicalUrl={canonicalURL} />
    <title>
      {
        frontmatter.title
          ? `${frontmatter.title} - ${CONFIG.SITE.title}`
          : CONFIG.SITE.title
      }
    </title>
    <style>
      body {
        width: 100%;
        display: grid;
        grid-template-rows: var(--theme-navbar-height) 1fr;
        --gutter: 0.5rem;
        --doc-padding: 2rem;
      }

      .layout {
        display: grid;
        grid-auto-flow: column;
        grid-template-columns:
          minmax(var(--gutter), 1fr)
          minmax(0, var(--max-width))
          minmax(var(--gutter), 1fr);
        overflow-x: hidden;
      }

      .grid-sidebar {
        position: sticky;
        top: 5rem;
        padding: 0;
      }

      #grid-left {
        position: fixed;
        background-color: var(--theme-navbar-bg);
        z-index: 10;
        display: none;
      }

      #grid-main {
        padding: var(--doc-padding) var(--gutter);
        grid-column: 2;
        display: flex;
        flex-direction: column;
        height: 100%;
      }

      #grid-right {
        display: none;
      }

      @media (min-width: 50em) {
        .layout {
          overflow: initial;
          grid-template-columns: 18rem minmax(0, var(--max-width));
          gap: 3rem;
        }

        .grid-sidebar {
          height: calc(100vh - 80px);
        }

        #grid-left {
          display: flex;
          position: sticky;
          grid-column: 1;
        }

        #grid-main {
          padding: var(--doc-padding) 3rem var(--doc-padding) var(--gutter);
        }
      }

      @media (min-width: 72em) {
        .layout {
          grid-template-columns: 18rem minmax(0, var(--max-width)) 18rem;
          gap: 4rem;
        }

        #grid-main {
          padding: var(--doc-padding) var(--gutter);
        }

        #grid-right {
          grid-column: 3;
          display: flex;
        }
      }
    </style>
    {rightSidebar === false && <style>
      @media (min-width: 72em) {
        .layout {
          grid-template-columns: 18rem minmax(0, calc(var(--max-width) + 5rem)) !important;
        }
      }
    </style>}
    <style is:global>
      .layout > * {
        width: 100%;
        height: 100%;
      }
      .mobile-sidebar-toggle {
        overflow: hidden;
      }
      .mobile-sidebar-toggle #grid-left {
        display: block !important;
        top: 2rem;
      }
    </style>
    <script>
      import 'media-chrome';
      import 'media-chrome/dist/menu/index';
      import 'media-chrome/dist/media-theme-element';
      import 'media-chrome/dist/experimental/index';
    </script>
  </head>
  <body>
    <Header currentPage={currentPage} />
    <main id="layout" class="layout">
      <aside id="grid-left" class="grid-sidebar" title="Site Navigation">
        <slot name="left-sidebar">
          <LeftSidebar currentPage={currentPage} />
        </slot>
      </aside>
      <div id="grid-main">
        <PageContent
          frontmatter={frontmatter}
          headings={headings}
          githubEditUrl={githubEditUrl}
          rightSidebar={rightSidebar}
        >
          <slot />
        </PageContent>
      </div>
      {rightSidebar !== false && <aside id="grid-right" class="grid-sidebar" title="Table of Contents">
        <RightSidebar headings={headings} githubEditUrl={githubEditUrl} />
      </aside>}
    </main>
    <Footer path={currentFile} />
  </body>
</html>
